<script setup name="frame">
import {ref, computed, reactive, onMounted} from 'vue'
import {
  Expand, Fold
} from '@element-plus/icons-vue'
import {useAuthStore} from "@/stores/auth.js";
import {useRouter} from "vue-router";
import authHttp from "@/api/authHttp.js";
import {ElMessage} from "element-plus";
import routes from "@/router/frame";

let displayuser = {
  department: {},
  realname: ""
}
const router = useRouter();
const authStore = useAuthStore();
let defaultActive = ref('home')
let isCollope = ref(false);
let dialogVisible = ref(false);
let resetPwdForm = reactive({
  oldpwd: '',
  pwd1: '',
  pwd2: '',
})
let isCollapse = ref(false);
let asideWidth = computed(() => {
  if (isCollapse.value) {
    return "64px"
  } else {
    return "250px"
  }
})
let formLabelWidth = "100px"
let formTag = ref()
const onCollapseAside = () => {
  isCollapse.value = !isCollapse.value
}

let resetPwdRoles = reactive({
  oldpwd: [
    {required: true, message: '请输入旧密码！', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度需在6~20之间！', trigger: 'blur'},
  ],
  pwd1: [
    {required: true, message: '请输入新密码！', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度需在6~20之间！', trigger: 'blur'},
  ],
  pwd2: [
    {required: true, message: '请输入确认密码！', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度需在6~20之间！', trigger: 'blur'},
  ]
})
onMounted(() => {
      defaultActive.value = router.currentRoute.value.name,
      displayuser.department = authStore.user.department,
      displayuser.realname = authStore.user.realname
})
const onExit = () => {
  authStore.clearUserToken()
  router.push({name: 'login'})
}
const onShowResetPwdDialog = () => {
  resetPwdForm.oldpwd = '';
  resetPwdForm.pwd1 = '';
  resetPwdForm.pwd2 = '';
  dialogVisible.value = true;
}
const onSubmit = () => {
  formTag.value.validate(async (valid, fileds) => {
    if (valid) {
      try {
        await authHttp.resetPwd(resetPwdForm.oldpwd, resetPwdForm.pwd1, resetPwdForm.pwd2);
        ElMessage.success("密码修改成功");
        dialogVisible.value = false;
      } catch (error) {
        ElMessage.error("修改失败！原因是" + error);
        dialogVisible.value = false;
      }
    } else {
      ElMessage.info("按照要求填写字段")
    }

  })
}
</script>

<template>
  <el-container class="container">
    <el-aside class="aside" :width="asideWidth">
      <router-link to="/" class="brand"><b>苍穹</b><span v-show="!isCollapse">OA系统</span></router-link>
      <el-menu :router="true" active-text-color="#ffd04b" background-color="#343a40" class="el-menu-vertical-demo"
               :default-active="defaultActive" text-color="#fff" :collapse="isCollapse" :collapse-transition="false">
        <template v-for="route in routes[0].children">
          <template v-if="authStore.has_permission(route.meta.permissions, route.meta.opt)">
            <el-menu-item v-if="!route.children" :index="route.name" :route="{ name: route.name }">
              <el-icon>
                <component :is="route.meta.icon"></component>
              </el-icon>
              <span>{{ route.meta.text }}</span>
            </el-menu-item>

            <el-sub-menu v-else :index="route.name">
              <template #title>
                <el-icon>
                  <el-icon>
                    <component :is="route.meta.icon"></component>
                  </el-icon>
                </el-icon>
                <span>{{ route.meta.text }}</span>
              </template>
              <template v-for="child in route.children">
                <template v-if="authStore.has_permission(child.meta.permissions, child.meta.opt)">
                  <el-menu-item v-if="!child.meta.hidden"
                                :index="child.name"
                                :route="{name: child.name}">
                    <el-icon>
                      <component :is="child.meta.icon"></component>
                    </el-icon>
                    <span>{{ child.meta.text }}</span>
                  </el-menu-item>
                </template>
              </template>
            </el-sub-menu>

          </template>
        </template>
      </el-menu>

    </el-aside>
    <el-container>
      <el-header class="header">
        <div class="lef-header">
          <el-button v-show="isCollapse" :icon="Expand" @click="onCollapseAside"/>
          <el-button v-show="!isCollapse" :icon="Fold" @click="onCollapseAside"/>
        </div>
        <div class="right-header">
          <el-dropdown>
              <span class="el-dropdown-link">
                <el-avatar :size="30" icon="UserFilled"/>
                <span
                    style="margin-left: 10px;">{{ displayuser.department.name }}-{{ displayuser.realname }}</span>
                <el-icon class="el-icon--right">
                  <arrow-down/>
                </el-icon>
              </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="onShowResetPwdDialog">修改密码</el-dropdown-item>
                <el-dropdown-item divided @click="onExit">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-main class="main">
        <RouterView></RouterView>
      </el-main>
    </el-container>
  </el-container>
  <el-dialog v-model="dialogVisible" title="修改密码" width="500">
    <el-form :model="resetPwdForm" :rules="resetPwdRoles" ref="formTag">
      <el-form-item label="旧密码" :label-width="formLabelWidth" prop="oldpwd">
        <el-input v-model="resetPwdForm.oldpwd" autocomplete="off" type="password"/>
      </el-form-item>

      <el-form-item label="新密码" :label-width="formLabelWidth" prop="pwd1">
        <el-input v-model="resetPwdForm.pwd1" autocomplete="off" type="password"/>
      </el-form-item>

      <el-form-item label="确认密码" :label-width="formLabelWidth" prop="pwd2">
        <el-input v-model="resetPwdForm.pwd2" autocomplete="off" type="password"/>
      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.container {
  height: 100vh;
}

.aside {
  background-color: #343a40;
}

.aside .brand {
  color: white;
  text-decoration: none;
  background-color: #231631;
  border-bottom: 1px solid #666666;
  display: flex;
  height: 60px;
  justify-content: center;
  align-items: center;
  font-size: 20px;
}

.header {
  border-bottom: white 1px solid;
  background-color: white;
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.main {
  background-color: #eff2ff;
}

.el-menu {
  border-right: none !important;
}

.header .el-dropdown-link {
  display: flex;
  align-items: center;
}

</style>